package com.iespring.service.app.comment;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iespring.bean.admin.goods.po.MallComment;
import com.iespring.bean.admin.user.entity.MallUser;
import com.iespring.bean.app.comment.bo.CommentPostBo;
import com.iespring.bean.app.comment.pojo.Comment;
import com.iespring.bean.app.comment.vo.CommentData;
import com.iespring.bean.app.comment.vo.CommentVoForPost;
import com.iespring.bean.app.comment.vo.CommentVoHasPic;
import com.iespring.bean.app.comment.pojo.MallCommentExample;
import com.iespring.bean.app.comment.vo.UserInfo;
import com.iespring.bean.common.BaseRespVo;
import com.iespring.bean.common.CommonData;
import com.iespring.mapper.admin.MallCommentMapper;
import com.iespring.util.UserInfoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author:ZhangShuPeng
 * @Date:2023/01/02/13:53
 * @Description:
 */
@Service
public class WxCommentServiceImpl implements WxCommentService {

    @Autowired
    MallCommentMapper mallCommentMapper;

    @Override
    public BaseRespVo list(int limit, int page, int valueId, Byte type, int showType) {


//        PageHelper.startPage(limit, page);
//        PageInfo<MallComment> mallCommentPageInfo = new PageInfo<>();

        MallUser user = UserInfoUtils.getUser();
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(user.getNickname());
        userInfo.setAvatarUrl(user.getAvatar());


        List<CommentVoHasPic> list = new ArrayList<>();

        Boolean hasPicture;
        if (showType == 1) {
            hasPicture = true;
        } else {
            hasPicture = null;
        }
        List<MallComment> mallComments = mallCommentMapper.selectAllComment(valueId, type, hasPicture);


        int total = mallComments.size();
        int pages;
        if (total % limit == 0) {
            pages = total / limit;
        } else {
            pages = (total / limit) + 1;
        }
//        List<MallComment> infoList = mallCommentPageInfo.getList();

        for (MallComment mallComment : mallComments) {
            CommentVoHasPic commentVoHasPic = new CommentVoHasPic();

            commentVoHasPic.setAddTime(mallComment.getAddTime());
            commentVoHasPic.setAdminCount(mallComment.getAdminContent());
            commentVoHasPic.setContent(mallComment.getContent());
            commentVoHasPic.setPicList(mallComment.getPicUrls());
            commentVoHasPic.setUserInfo(userInfo);

            list.add(commentVoHasPic);
        }
//        for (MallComment mallComment : mallComments) {
//
//        }

        CommonData data = new CommonData();
        data.setList(list);
        data.setTotal(total);
        data.setPages(pages);
        data.setLimit(limit);
        data.setPage(page);
        BaseRespVo baseRespVo = BaseRespVo.ok(data);
        return baseRespVo;

    }

    @Override
    public BaseRespVo count(int valueId, byte type) {

        MallCommentExample mallCommentExample = new MallCommentExample();
        MallCommentExample.Criteria criteria = mallCommentExample.createCriteria();
        criteria.andTypeEqualTo((byte) 0);
        criteria.andValueIdEqualTo(valueId);
        List<MallComment> mallComments = mallCommentMapper.selectByValueId(valueId);
        int hasPicCount = 0;
        for (MallComment mallComment : mallComments) {
            if (mallComment.getHasPicture() == true) {
                hasPicCount++;
            }
        }
        CommentData data = new CommentData(mallComments.size(), hasPicCount);

        BaseRespVo baseRespVo = BaseRespVo.ok(data);

        return baseRespVo;
    }

    @Override
    public BaseRespVo post(CommentPostBo commentPostBo) {

        MallUser user = UserInfoUtils.getUser();

        Comment mallComment = new Comment();
        mallComment.setValueId(commentPostBo.getValueId());
        mallComment.setType(commentPostBo.getType());
        mallComment.setContent(commentPostBo.getContent());
        mallComment.setHasPicture(commentPostBo.getHasPicture());
        mallComment.setPicUrls(commentPostBo.getPicUrls());
        mallComment.setUserId(user.getId());
//        if (commentPostBo.getPicUrls().length == 0) {
//            mallComment.setHasPicture(false);
//            mallComment.setPicUrls(commentPostBo.getPicUrls());
//        } else {
//            mallComment.setHasPicture(true);
//        }
        mallComment.setStar(commentPostBo.getStar());
        mallComment.setAddTime(new Date());
        mallComment.setUpdateTime(new Date());

        mallCommentMapper.insertSelective(mallComment);

        CommentVoForPost commentVoForPost = new CommentVoForPost();
        commentVoForPost.setAddTime(mallComment.getAddTime());
        commentVoForPost.setContent(mallComment.getContent());
        commentVoForPost.setHasPicture(mallComment.getHasPicture());
        commentVoForPost.setId(mallComment.getId());
        commentVoForPost.setPicUrls(mallComment.getPicUrls());
        commentVoForPost.setStar(mallComment.getStar());
        commentVoForPost.setType(mallComment.getType());
        commentVoForPost.setUpdateTime(mallComment.getUpdateTime());
        commentVoForPost.setUserId(mallComment.getUserId());
        commentVoForPost.setValueId(mallComment.getValueId());

        BaseRespVo baseRespVo = BaseRespVo.ok(commentVoForPost);

        return baseRespVo;
    }


}
